﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Xml.Linq;
using System.Linq;
using System;
using System.Diagnostics;

namespace ryan
{
    class MapData
    {
        public string fond { get; set; }
        public string sol { get; set; }
        public string nom { get; set; }
        public int numLevel, height, width;
    }
    class Map
    {
        private Texture2D mSpriteObjet, mSpriteFond, mSpriteSol;
        public Rectangle Size;
        public float Scale = 1.0f;
        public Vector2 Position = new Vector2(0, 0);
        public string fondMap,nomXml;
        private MapData _mapData;
        public SpriteFont fontImpact;
        public int frameLine, width;

        public Map(string nomXml)
        {

            this.nomXml = "Map/"+nomXml;
            this.fondMap = "Test";
            this.frameLine = 0;
        }
        public void LoadContent(ContentManager theContentManager)
        {

            this.fontImpact = theContentManager.Load<SpriteFont>("SpriteFont_Impact");
            /*XElement myElement = XElement.Load("Map/level1.xml").Descendants("Level").Last();
            this.fondMap = (string)myElement.Attribute("fond");*/
            //_mapData.fond = this.fondMap;
            _mapData = (from level in XElement.Load(this.nomXml).Descendants("Level")
                        where (int)level.Attribute("Number") == 1
                        select new MapData
                        {
                            numLevel = (int)level.Attribute("Number"),
                            height = (int)level.Attribute("Height"),
                            width = (int)level.Attribute("Width"),
                            fond = (string)level.Attribute("fond"),
                            nom = (string)level.Attribute("nom"),
                            sol = (string)level.Attribute("sol")
                        }).SingleOrDefault();
           // this.fondMap = GetCurrentLevelData().GetType().ToString();

            this.width = this._mapData.width;
            mSpriteFond = theContentManager.Load<Texture2D>(this._mapData.fond);
            mSpriteSol = theContentManager.Load<Texture2D>(this._mapData.sol);
            /*mSpriteFond = theContentManager.Load<Texture2D>(fondMap);
            mSpriteObjet = theContentManager.Load<Texture2D>("sprite-objet");
            Size = new Rectangle(0, 0, (int)(mSpriteObjet.Width * Scale), (int)(mSpriteObjet.Height * Scale));*/
        }
        internal MapData GetCurrentLevelData()
        {
            return _mapData;
        }
        public void Update(MouseState mouse, KeyboardState keyboard,GamePadState gamepad, GameTime gameTime)
        {
            if (keyboard.IsKeyDown(Keys.Left))
            {
                
                this.frameLine = this.frameLine + 2;
            }
            if (keyboard.IsKeyDown(Keys.Right))
            {
                this.frameLine = this.frameLine -2;
            }
        }
        public void Draw(SpriteBatch theSpriteBatch)
        {
            theSpriteBatch.DrawString(this.fontImpact, this.fondMap, new Vector2(100, 100), Color.Black);
            theSpriteBatch.Draw(mSpriteFond, new Vector2(0, 0), Color.White);
            int nbSprite = (int)(this._mapData.width / 30) + 1;
            for (int i = 0; i < nbSprite; i++)
            {
                //Sol 
                theSpriteBatch.Draw(mSpriteSol, new Rectangle((i * 30)+this.frameLine, 434, 30, 30), new Rectangle(0,0,30,30),Color.White);
                // Terre
                theSpriteBatch.Draw(mSpriteSol, new Rectangle((i * 30) + this.frameLine, 464, 30, 30), new Rectangle(0, 30, 30, 30), Color.White);
            }
            /*theSpriteBatch.Draw(mSpriteTexture, Position,
                new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height), Color.White,
                0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);*/
        }
    }
}
